package com.owen.liferay.jdo.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

@PersistenceCapable(detachable="true", table="team")
@Version(column = "modifiedDate", strategy = VersionStrategy.DATE_TIME, extensions = { @Extension(vendorName = "DataNucleus", key = "field-name", value = "modifiedDate") })
public class Team {

	final static PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
	
	@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
	@PrimaryKey
	private long teamId;
	
	@Persistent
	private Date modifiedDate;
	
	@Persistent
	private String name;
	
	@Persistent
	private String description;
	
	public static List<Team> findTeams(List<Long> teamIds) {
		List<Team> teams = new ArrayList<Team>();
		
		for (Long id : teamIds) {
			teams.add(findTeam(id));
		}
		
		return teams;
	}
	
	public static Team findTeam(long id) {
		
		System.out.println("findTeam is: " + id);

		PersistenceManager pm = PMF.getPersistenceManager();

		Transaction tx = pm.currentTransaction();

		Team team = null;
		
		Team detached = null;

		try {
			tx.begin();

			team = pm.getObjectById(Team.class, id);
			
			detached = pm.detachCopy(team);

			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return detached;
	}

	public long getTeamId() {
		return teamId;
	}

	public void setTeamId(long teamId) {
		this.teamId = teamId;
	}

	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
}
